package graduation.project.store;

import graduation.project.body.Result;
import graduation.project.body.Results;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

public class FileResults implements IResults {

	public Results getResults(String resultsID) {
		Results results = null;
		try{
			String originExID = resultsID.substring(0, resultsID.indexOf('_'));
			File resultsFile = new File("D:\\data\\" + originExID + "\\results\\" + resultsID);
			BufferedReader in = new BufferedReader(new FileReader(resultsFile));
			ArrayList<String> contents = new ArrayList<String>();
			
			ArrayList<Result> records = new ArrayList<Result>();
			
			String aLine;
			while((aLine = in.readLine()) != null)
				contents.add(aLine);
			
			String originEx = contents.get(1);
			String mutantsID = contents.get(2);
			String testSetID = contents.get(3);
			
			Result r;
			int colonPos;
			for(int i = 3; i < contents.size(); i++){
				aLine = contents.get(i);
				colonPos = aLine.indexOf(":");
				r = new Result(aLine.substring(0, colonPos), aLine.substring(colonPos + 1, aLine.length()).equals("true")?true:false);
				records.add(r);
			}
			
			results = new Results(originEx, resultsID, mutantsID, testSetID);
			results.setRecords((Result[])records.toArray());
			in.close();
		}catch(FileNotFoundException e) {
			System.out.println("The file does not exist");
			e.printStackTrace();
		}catch(IOException e){
			System.out.println("Occurring error when read file");
			e.printStackTrace();
		}
		return results;
	}

	public int insertResults(Results r) {
		int result = 0;
		if(r == null)
			return 0;
		try{
			String resultsID = r.getResultsID();
			String originEx = r.getOriginEx();
			String mutantsID = r.getMutantsID();
			String testSetID = r.getTestSetID();
			Result[] records = r.getRecords();
			
			File dir = new File("D:\\data\\" + originEx.hashCode() + "\\results");
			dir.mkdirs();
			File resultsFile = new File("D:\\data\\" + originEx.hashCode() + "\\results\\" + resultsID);
			
			if( ! resultsFile.createNewFile()){
				System.out.println("the file exists.");
				return 0;
			}
			
			PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(resultsFile)));
			out.println(resultsID);
			out.println(originEx);
			out.println(mutantsID);
			out.println(testSetID);
			
			Result aResult;
			for(int i = 0; i < records.length; i++){
				aResult = records[i];
				if(aResult == null){
//					System.out.println("in null");
					continue;
				}
				out.println(aResult.getExpression() + ":" + aResult.isDetected());
			}
			
			result = 1;
			out.close();
		}catch(IOException e){
			System.out.println("occurring error when write the file.");
			e.printStackTrace();
		}
			
		return result;
	}

	public int modifyResults(Results r) {
		// TODO Auto-generated method stub
		return 0;
	}

}
